package com.mi.proxy4;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class Test4 {

	public static void main(String[] args) throws Exception {
		Class<?>[] interfaces =new Class<?>[] {Singer.class,Person.class};
		Object obj=Proxy.newProxyInstance(Test2.class.getClassLoader(), interfaces, new FirstInvocationHandler());
		System.out.println(obj.getClass());
		System.out.println(obj instanceof Person);
		System.out.println(obj instanceof Singer);
		Singer singer =(Singer)obj;
		Person person =(Person)obj;
		singer.sing("小苹果 ");
		singer.sign();
		singer.work();
		int index =person.lives();
		System.out.println(index);
	}
}
class FirstInvocationHandler implements InvocationHandler {

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

		String methodName =method.getName();
		switch (methodName) {
		case "sing":
			System.out.println(args[0]);
			System.out.println("我不会唱歌，"+args[0]+"我只是你一个码农而已");
			return null;
		case "sign":
			System.out.println("今天终于找到工作啦，和一家软件公司签约啦");
			return null;
		case "work":
			System.out.println("还在工地搬砖呢");
			return null;
		case "lives":
			System.out.println("单身狗");
			return 0;
		}
	
		return null;
	}
	
}
